package gae.util.http.client;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;

public class HttpRequest {
    
    private HttpURLConnection connection = null;
    
    private String method = "GET";
    private String url = null;
    private Parameter param = null;
    private Header header = null;
    
    
    public HttpRequest(String method, String url) {
        this.method = method;
        this.url = url;
    }
    
    public void setMethod(String method) {
        this.method = method;
    }

    public void setRequestUrl(String url) {
        this.url = url;
    }

    public void setParameter(Parameter param) {
        this.param = param;
    }

    public void setHeader(Header header) {
        this.header = header;
    }

    public HttpResponse execute() throws IOException {
        if (method == null) {
            return null;
        }
        if ("GET".equals(method)) {
            return executeGET();
        } else {
            return executePOST();
        }
    }
    
    protected HttpResponse executeGET() throws IOException {
        
        // Set Request Parameters
        String urlParam = url;
        if (param != null && param.hasParameter()) {
            urlParam = urlParam + "?" + param.toParameterString();
        }
        URL requestUrl = new URL(urlParam);
        connection = (HttpURLConnection)requestUrl.openConnection();
        connection.setRequestMethod(method);
        connection.setInstanceFollowRedirects(false);
        
        // Set Request Headers
        if (header != null) {
            Iterator<String> ite = header.keys().iterator();
            while (ite.hasNext()) {
                String key = ite.next();
                Iterator<String> values = header.get(key).iterator();
                while (values.hasNext()) {
                    String value = values.next();
                    connection.addRequestProperty(key, value);
                }
            }
        }
        
        HttpResponse res = new HttpResponse();
        res.setConnection(connection);
        
        return res;
    }
    
    protected HttpResponse executePOST() throws IOException {
        URL requestUrl = new URL(url);
        connection = (HttpURLConnection)requestUrl.openConnection();
        connection.setRequestMethod(method);
        connection.setInstanceFollowRedirects(false);
        
        // Set Request Headers
        if (header != null) {
            Iterator<String> ite = header.keys().iterator();
            while (ite.hasNext()) {
                String key = ite.next();
                Iterator<String> values = header.get(key).iterator();
                while (values.hasNext()) {
                    connection.addRequestProperty(key, values.next());
                }
            }
        }
        
        // Set Request Paramaters
        connection.setDoOutput(true);
        if (param != null) {
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            if (param.hasParameter()) {
                writer.write(param.toParameterString());
                writer.close();
            } else {
                writer.close();
            }
        }
        
        HttpResponse res = new HttpResponse();
        res.setConnection(connection);
        
        return res;
    }
}
